package com.wangyadong.hobby.schedule.jobtask.thread;

import java.util.concurrent.TimeUnit;

public class VolatileText4 {

    private static  int INTT_VALUE = 0;
    private static int LIMIT = 100;

    public static void main(String[] args) {
        test();
    }

    private static void test() {
        new Thread(() -> {
            int localVlaue = INTT_VALUE;
            while (localVlaue < LIMIT) {
                if (localVlaue != INTT_VALUE) {
                    System.out.printf("reader value [%d]\n", INTT_VALUE);
                    localVlaue = INTT_VALUE;
                }
            }
        }, "reader").start();
        new Thread(() -> {
            int localVlaue = INTT_VALUE;
            while (INTT_VALUE < LIMIT) {
                System.out.printf(" update value [%d]\n", ++localVlaue);
                INTT_VALUE = localVlaue;
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "update").start();
    }
}
